<template>
  <div class="header">
    <div class="left">
      <span class="iconfont iconjiantou" @click="$router.back()"></span>
      <span class="iconfont iconnew"></span>
    </div>
    <div v-if="post.type != 2">
      <div class="btnFollowActive" v-if="!post.has_follow" @click="follow">关注</div>
      <div class="btnFollow" v-else-if="post.has_follow" @click="unfollow">已关注</div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["post"],
  methods: {
    follow() {
      // 这里我应该发送 ajax 请求关注用户
      this.$axios({
        url: "/user_follows/" + this.post.user.id,
        method: "get"
      }).then(res => {
        const { message } = res.data;
        // console.log(message);
        // 如果我们点击了关注,返回的 message == '关注成功'
        // 那么这个请求就算完成了

        if (message == "关注成功") {
          this.post.has_follow = true;
        }
      });
    },
    unfollow() {
      // 这里我应该发送 ajax 请求取消关注用户
      this.$axios({
        url: "/user_unfollow/" + this.post.user.id,
        method: "get"
      }).then(res => {
        const { message } = res.data;
        // console.log(message);
        // 如果我们点击了已关注,返回的 message == '取消关注成功'
        // 那么这个请求就算完成了

        if (message == "取消关注成功") {
          this.post.has_follow = false;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.header {
  display: flex;
  justify-content: space-between;
  padding: 0 2.778vw;
  align-items: center;
  height: 13.889vw;
  .left {
    display: flex;
    align-items: center;
    .iconnew {
      font-size: 15vw;
      margin-left: 1.389vw;
    }
  }
  .btnFollowActive {
    font-size: 3.333vw;
    height: 6.667vw;
    line-height: 6.667vw;
    padding: 0 4.167vw;
    color: white;
    background-color: red;
    border: 1px solid red;
    border-radius: 3.333vw;
  }
  .btnFollow {
    font-size: 3.333vw;
    height: 6.667vw;
    line-height: 6.667vw;
    padding: 0 4.167vw;
    color: #888;
    // background-color: red;
    border: 1px solid #888;
    border-radius: 3.333vw;
  }
}
</style>